package renamer;

import static eabox.FileUtil.MUSIC_RELATE_FILE_PATTERN;
import static eabox.FileUtil.getFileExt;
import static eabox.FileUtil.getFilesList;
import static eabox.FileUtil.getParentName;

import java.io.File;
import java.io.FileNotFoundException;

public class MusicFileRenamer implements FileRenamer {

	private File canonicalMusicFile(File old) {
		return new File(old.getParent() + "\\" + getParentName(old)
				+ getFileExt(old));
	}

	private void renameToCanonicalMusicFile(File file) {
		file.renameTo(canonicalMusicFile(file));
	}

	public void rename(File dir) {
		try {

			if (!dir.exists())
				throw new FileNotFoundException();

			if (dir.isDirectory())
				for (File file : getFilesList(dir, MUSIC_RELATE_FILE_PATTERN))
					renameToCanonicalMusicFile(file);
			else
				renameToCanonicalMusicFile(dir);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}